package com.business.user.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.business.common.dto.R;
import com.business.common.enums.BusinessExceptionEnum;
import com.business.common.enums.ErrorEnum;
import com.business.common.exception.BusinessException;
import com.business.common.exception.CustomException;
import com.business.common.utils.PageUtils;
import com.business.common.utils.StrKit;
import com.business.redis.constant.RedisKeyConstant;
import com.business.redis.utils.RedisKeysUtil;
import com.business.redis.utils.RedisUtil;
import com.business.user.entity.Message;
import com.business.user.entity.vo.MessageVo;
import com.business.user.mapper.MessageMapper;
import com.business.user.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 消息表:业务接口实现类
 *
 * @author chenjiehong
 * @date 2019-11-29 14:28:27
 * @since 1.0
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    @Transactional
    public Message add(Message message) {
        if (message.getUserId() == null) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "userId");
        }
        if (message.getType() == null) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "type");
        }
        if (StrKit.isBlank(message.getContent())) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "content");
        }
        if (message.getFromUserId() == null && message.getType() == 1) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "fromUserId");
        } else if (message.getFromUserId() == null) {
            message.setFromUserId(0);//系统用户
        }
        if (message.getContent().length() > 500) {
            throw new BusinessException(BusinessExceptionEnum.MESSAGE_COMMENT_LENGTH_ERROR);
        }
        message.setIsRead(0);
        message.setCreateTime(new Date());
        messageMapper.insert(message);

        Set<String> keys = new HashSet<>();
        String key2 = String.format(RedisKeyConstant.MESSAGE_COUNT,message.getUserId());
        String key3 = String.format(RedisKeyConstant.MESSAGE_COUNT_BY_TYPE,message.getUserId());
        keys.add(key2);
        keys.add(key3);

        RedisUtil.delValue(keys);
        return message;
    }

    @Override
    public Message query(Integer id) {
        Message message = messageMapper.selectById(id);
        return message;
    }

    @Override
    public PageUtils queryPage(Page<Message> page, Integer userId, Integer type, Integer isAll) {
        if (type == null) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "type");
        }
        Wrapper<Message> wrapper = new EntityWrapper<>();
        wrapper.eq("m.user_id", userId);
        wrapper.eq("m.type", type);
        if (isAll == null || isAll == 0) {
            wrapper.eq("m.is_read", 0);
        }
        wrapper.orderBy("create_time", false);
        List<MessageVo> result = messageMapper.queryPage(page, wrapper);
        PageUtils pageUtils = new PageUtils(result, page.getTotal());
        return pageUtils;
    }

    @Override
    public PageUtils queryPageByOwn(Page<Message> page, Integer userId, Integer type, Integer isAll) {
        if (type == null) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "type");
        }
        Wrapper<Message> wrapper = new EntityWrapper<>();
        wrapper.eq("m.from_user_id", userId);
        wrapper.eq("m.type", type);
        if (isAll == null || isAll == 0) {
            wrapper.eq("m.is_read", 0);
        }
        wrapper.orderBy("create_time", false);
        List<MessageVo> result = messageMapper.queryPageByOwn(page, wrapper);
        PageUtils pageUtils = new PageUtils(result, page.getTotal());
        return pageUtils;
    }

    @Override
    @Transactional
    public Integer updateState(Integer id, Integer isRead) {
        Message message = new Message();
        message.setId(id);
        message.setIsRead(isRead);

        return messageMapper.updateById(message);
    }

    @Override
    public Integer getCount(Integer userId) {
        Integer count = messageMapper.selectCount(new EntityWrapper<Message>().eq("user_id", userId).eq("is_read", 0));
        return count;
    }

    @Override
    public List<Map<String, Integer>> getCountByType(Integer userId) {
        List<Map<String, Integer>> resultMap = messageMapper.getCountByType(userId);
        return resultMap;
    }


}